<template>
  <div class="p-2" style="overflow-y: hidden;">
    <el-form ref="dictFormRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="字典名称" prop="dictName">
        <el-input v-model="form.dictName" placeholder="请输入字典名称" />
      </el-form-item>
      <el-form-item label="字典类型" prop="dictType">
        <el-input v-model="form.dictType" placeholder="请输入字典类型" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
      </el-form-item>
    </el-form>

    <div style="text-align: center; margin-top: 20px;">
        <el-button v-loading="submitLoading" :disabled="submitLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button v-loading="submitLoading" :disabled="submitLoading" @click="emit('close')">取 消</el-button>
      </div>
  </div>
</template>

<script setup lang="ts">
import { addType, getType, updateType } from '@/api/system/dict/type';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const props = defineProps({
  dictId: {
    type: Number || String,
    required: true
  }
});

const emit = defineEmits(['close', 'success']);

const dictFormRef = ref<ElFormInstance>();

const form = ref<any>({
  dictId: undefined,
  dictName: '',
  dictType: '',
  remark: ''
})
const rules = ref<any>({
  dictName: [{ required: true, message: '字典名称不能为空', trigger: 'blur' }],
  dictType: [{ required: true, message: '字典类型不能为空', trigger: 'blur' }]
})

/**
 * 获取详情
 */
const getInfo = async ()=> {
  proxy.$modal.loading('加载中...')
  const res = await getType(props.dictId).finally(() => proxy?.$modal.closeLoading())
  Object.assign(form.value, res.data);
}

/** 提交按钮 */
const submitLoading = ref(false);
const submitForm = () => {
  dictFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      submitLoading.value = true;
      await updateType(form.value).finally(() => { submitLoading.value = false })
      proxy?.$modal.msgSuccess('操作成功');

      emit('close')
      emit('success')
    }
  });
};

onMounted(() => {
  getInfo()
});
</script>
